package com.swak.frame.monitor.async;

import java.util.concurrent.TimeUnit;

import org.slf4j.Logger;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import com.swak.frame.aware.MonitorAware;
import com.swak.frame.logger.SwakLoggerFactory;
import com.swak.frame.timer.WheelTimerHolder;

public class AsyncThreadPoolMonitor implements MonitorAware {

    private ThreadPoolTaskExecutor taskExecutor;
    private IThreadPoolMonitorService threadPoolMonitorService;
    private Logger logger = SwakLoggerFactory.getLogger(AsyncThreadPoolMonitor.class);

    private long monitoringPeriod;

    @Override
    public long getMonitorPeriod() {
        return this.monitoringPeriod;
    }

    @Override
    public void startup() {
        logger.warn("[swak-AsyncThreadPool] - AsyncThreadPool监控系统启动  ......");
        ThreadPoolMonitorTask task = new ThreadPoolMonitorTask(taskExecutor, getMonitorPeriod());
        WheelTimerHolder.monitorWheel().newTimeout(task, monitoringPeriod, TimeUnit.SECONDS);
    }

    public ThreadPoolTaskExecutor getTaskExecutor() {
        return taskExecutor;
    }

    public void setTaskExecutor(ThreadPoolTaskExecutor taskExecutor) {
        this.taskExecutor = taskExecutor;
    }

    public IThreadPoolMonitorService getThreadPoolMonitorService() {
        return threadPoolMonitorService;
    }

    public void setThreadPoolMonitorService(IThreadPoolMonitorService threadPoolMonitorService) {
        this.threadPoolMonitorService = threadPoolMonitorService;
    }

    public void setMonitoringPeriod(long monitoringPeriod) {
        this.monitoringPeriod = monitoringPeriod;
    }

}
